using System;
using System.Data;
using DotNetNuke.Framework.Providers;
using Microsoft.ApplicationBlocks.Data;

namespace YourCompany.Modules.Cafeteria
{
	public class SqlDataProvider : DataProvider
	{


		#region vars

		private const string providerType = "data";
		private const string moduleQualifier = "YourCompany_";

		private ProviderConfiguration providerConfiguration = ProviderConfiguration.GetProviderConfiguration(providerType);
		private string connectionString;
		private string providerPath;
		private string objectQualifier;
		private string databaseOwner;

		#endregion

		#region cstor

		/// <summary>
		/// cstor used to create the sqlProvider with required parameters from the configuration
		/// section of web.config file
		/// </summary>
		public SqlDataProvider()
		{
			Provider provider = (Provider)providerConfiguration.Providers[providerConfiguration.DefaultProvider];
			connectionString = DotNetNuke.Common.Utilities.Config.GetConnectionString();

			if (connectionString == string.Empty)
				connectionString = provider.Attributes["connectionString"];

			providerPath = provider.Attributes["providerPath"];

			objectQualifier = provider.Attributes["objectQualifier"];
			if (objectQualifier != string.Empty && !objectQualifier.EndsWith("_"))
				objectQualifier += "_";

			databaseOwner = provider.Attributes["databaseOwner"];
			if (databaseOwner != string.Empty && !databaseOwner.EndsWith("."))
				databaseOwner += ".";
		}

		#endregion

		#region properties

		public string ConnectionString
		{
			get { return connectionString; }
		}


		public string ProviderPath
		{
			get { return providerPath; }
		}

		public string ObjectQualifier
		{
			get { return objectQualifier; }
		}


		public string DatabaseOwner
		{
			get { return databaseOwner; }
		}

		#endregion

		#region private methods

		private string GetFullyQualifiedName(string name)
		{
			return DatabaseOwner + ObjectQualifier + moduleQualifier + name;
		}

		private object GetNull(object field)
		{
			return DotNetNuke.Common.Utilities.Null.GetNull(field, DBNull.Value);
		}

		#endregion

		#region override methods

		public override IDataReader GetCafeteriaList()
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaItemListSelect");
		}

		public override IDataReader GetCafeteria(int ItemID)
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaItemSelect", ItemID);
		}

		public override void AddCafeteria(string ItemName, int ItemPrice)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaItemInsert", ItemName, ItemPrice);
		}

		public override void UpdateCafeteria(int ItemID, string ItemName, int ItemPrice)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaItemUpdate", ItemID, ItemName, ItemPrice);
		}

		public override void DeleteCafeteria(int ItemID)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaItemDelete", ItemID);
		}

		public override IDataReader GetOrderList()
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaOrderListSelect");
		}

		public override IDataReader GetOrder(int OrderID)
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaOrderSelect", OrderID);
		}

		public override void AddOrder(int ItemID, int ItemQuantity, string Room, int TotalPrice, DateTime Time, string ByUser, bool Completed)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaOrderInsert", ItemID, ItemQuantity, Room, TotalPrice, Time, ByUser, Completed);
		}

		public override void UpdateOrder(int OrderID, int ItemID, int ItemQuantity, string Room, int TotalPrice, DateTime Time, string ByUser, bool Completed)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaOrderUpdate", OrderID, ItemID, ItemQuantity, Room, TotalPrice, Time, ByUser, Completed);
		}

		public override void DeleteOrder(int OrderID)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaOrderDelete", OrderID);
		}

		public override IDataReader GetAnnouncementList()
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaAnnouncementListSelect");
		}

		public override IDataReader GetAnnouncement(int NewsID)
		{
			return (IDataReader)SqlHelper.ExecuteReader(connectionString, "CafeteriaAnnouncementSelect", NewsID);
		}

		public override void AddAnnouncement(DateTime Time, string Title, string Detail)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaAnnouncementInsert", Time, Title, Detail);
		}

		public override void UpdateAnnouncement(int NewsID, DateTime Time, string Title, string Detail)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaAnnouncementUpdate", NewsID, Time, Title, Detail);
		}

		public override void DeleteAnnouncement(int NewsID)
		{
			SqlHelper.ExecuteNonQuery(connectionString, "CafeteriaAnnouncementDelete", NewsID);
		}

		#endregion
	}
}
